package com.spider.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.spider.dto.Media;

/**
 * 
 * @author wangyc
 * @date 2015-2-16
 */
public class ParserList {
	private List<Media> medias = new ArrayList<Media>();

	public static String readFile(String szFileName) {
		try {
			BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream(new File(szFileName)), "utf-8"));
			String szContent = "";
			String szTemp;

			while ((szTemp = bis.readLine()) != null) {
				szContent += szTemp + "\n";
			}
			bis.close();
			return szContent;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/*
	 */
	public void testNodeFilter() {
		try {
			String content = readFile("list.html");
			NodeFilter filter = new TagNameFilter("a");
			Parser parser = new Parser(content);
			parser.setEncoding("UTF-8");
			NodeList list = parser.extractAllNodesThatMatch(filter);
			for (int i = 0; i < list.size(); i++) {
				Media media = new Media();
				LinkTag linkTag = (LinkTag) list.elementAt(i);
				media.setMediaName(linkTag.getLinkText());
				media.setName(linkTag.getLink().substring(0, linkTag.getLink().indexOf(".")).replaceAll("-", "").replaceAll("_", ""));
				medias.add(media);
			}
		} catch (Exception e) {
		}
	}

	public List<Media> mediaList() {
		testNodeFilter();
		List<Media> medias = this.medias;
		return medias;
	}

	public static void main(String[] args) {
		ParserList parserList = new ParserList();
		for (Media media : parserList.mediaList()) {
			System.out.println(media.getMediaName() + " , " + media.getName());
		}
	}

	public List<Media> getMedias() {
		return medias;
	}

	public void setMedias(List<Media> medias) {
		this.medias = medias;
	}
}
